package com.neusoft.resturant.common.exception;

import com.neusoft.resturant.common.utils.R;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 业务异常
     */
    @ExceptionHandler(BizException.class)
    public R bizExceptionHandler(BizException e) {
        System.out.println("应用抛出BizException异常");
        System.out.println("exception code:"+e.getCode());
        System.out.println("exception msg:"+e.getMsg());

        return R.error(e.getCode(), e.getMsg());
    }

    /**
     * 路径不匹配异常
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    public R handlerNoFoundException(Exception e) {
        return R.error(404, "路径不存在，请检查路径是否正确");
    }

    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public R notFount(RuntimeException e) {
        System.out.println(e);
        return R.error("运行时异常:" + e.getMessage());
    }

    /**
     * 基类异常
     */
    @ExceptionHandler(Exception.class)
    public R exceptionHandler(Exception e) {
        return R.error(e.getMessage());
    }

}
